package ru.pb.game.network.engine.client.packets.sendable;

import ru.pb.game.network.engine.client.packets.GameServerToClientPacket;
import ru.pb.game.objects.model.chat.Chat;
import ru.pb.game.objects.model.players.Player;

public class PROTOCOL_LOBBY_CHATTING_ACK extends GameServerToClientPacket
{
	private int _playerNameLen, _chatTextLen, _chatType;
	private String _playerName, _chatText;

	public PROTOCOL_LOBBY_CHATTING_ACK(Player player, Chat chat)
	{
		_chatType = chat.getChatTypeId();

		_playerName = player.getName();
		if(_playerName.length() > Player.NET_NICK_NAME_SIZE)
		{
			_playerNameLen = Player.NET_NICK_NAME_SIZE; // длина имени игрока
			_playerName = player.getName().substring(0, Player.NET_NICK_NAME_SIZE); // Обрезаем до максимума.
		}
		else
		{
			_playerNameLen = player.getName().length();
		}

		_chatText = chat.getText();
		if(_chatText.length() > Chat.NET_MAX_LEN)
		{
			_chatTextLen = Chat.NET_MAX_LEN;
			_chatText = chat.getText().substring(0, Chat.NET_MAX_LEN); // Обрезаем до максимума.
		}
		else
		{
			_chatTextLen = _chatText.length();
		}
	}

	/**
	 * 0x24, 0x00,
	 * 0x17, 0x0b,
	 * 0x8b, 0x5c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
	 * 0x0d, // lenght nickname (max 33)
	 * 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 0x4d, 0x4d, 0x41, 0x54, 0x4f, 0x52, 0x00, 0x00, // nickname
	 * 0x0b, 0x00, // lenght text message (max 256)
	 * 0x48, 0x69, 0x20, 0x74, 0x6f, 0x20, 0x61, 0x6c, 0x6c, 0x21, 0x00 // text
	 */

	@Override
	public void writeImpl()
	{
		writeH(2839);
		writeB(new byte[] {(byte) 0x8b, 0x5c, 0x10, 0x10});
		writeC(_playerNameLen);
		writeS(_playerName); // имя игрока
		writeC(); // COLOR ? // TODO text clor??
		writeH(_chatTextLen); // Длина сообщения
		writeS(_chatText); // Cообщение
		writeC(_chatType);
	}
}